/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tracker.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Workshop Class ID : SE0691 Student ID : SE90009 Student Name : Phạm Nguyễn
 * Nhật Trung Due Date : 12 November 2012 I declare that this assignment is my
 * own work in accordance with FPT Policy.
 */
@Stateless
public class UsersEntityFacade extends AbstractFacade<UsersEntity> {

    @PersistenceContext(unitName = "ASG2-Tracker1-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsersEntityFacade() {
        super(UsersEntity.class);
    }

    public boolean checkLogin(String userName, String password) {
        Query query = em.createNamedQuery("UsersEntity.checkLogin");
        query.setParameter("userName", userName);
        query.setParameter("password", password);
        List list = query.getResultList();
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    public List getUsersByName(String username) {
        Query query = em.createNamedQuery("UsersEntity.findByUserName");
        query.setParameter("userName", username);
        List list = query.getResultList();
        return list;
    }

    public List getUsersByLevel(int level) {
        Query query = em.createNamedQuery("UsersEntity.findByUserLevel");
        query.setParameter("userLevel", level);
        List list = query.getResultList();
        return list;
    }

    public boolean checkIsExistedUsername(String username) {
        List list = getUsersByName(username);
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    public int checkLoginReturnUserId(String userName, String password) {
        List<UsersEntity> list = getUsersByName(userName);
        if (!list.isEmpty()) {
            Iterator<UsersEntity> it = list.iterator();
            while (it.hasNext()) {
                UsersEntity entity = it.next();
                if (entity.getPassword().equals(password)) {
                    return entity.getUserId();
                }
            }
        }
        return -1;
    }

    public List findAllUsersByUserName(String userName) {
        List newLst = new ArrayList();
        try {
            List<UsersEntity> list = findAll();

            if (!list.isEmpty()) {
                Iterator<UsersEntity> it = list.iterator();
                while (it.hasNext()) {
                    UsersEntity entity = it.next();
                    if (entity != null) {
                        System.out.println("In User: " + entity.getUserName().toLowerCase().contains(userName));
                        System.out.println("String: '" + userName + "'");
                        System.out.println("Entity: '" + entity.getUserName() + "'");
                        if (entity.getUserName().toLowerCase().contains(userName)) {
                            newLst.add(entity);
                            System.out.println("Added in Users");
                        }
                    }

                }
            }
        } catch (Exception e) {
            System.out.println("Error when find Users by Name: " + e.getMessage());
        }

        return newLst;
    }
}
